<include file="public@header"/>
<link href="__TMPL__/public/assets/themes/{:cmf_get_admin_style()}/house.css" rel="stylesheet">
</head>
<body>
<div class="wrap js-check-wrap">
    <div class="house-password">
      <div class="header">
        <form class="form-inline add-form" action="addForm.html" name="add" method="post">
          <input type="text" class="add-text form-control" name="password" value="" autocomplete="off" placeholder="新增密码">
          <button class="btn btn-primary" type="button" name="button" onclick="addPassword()">确认增加</button>
          <span class="add-message"></span>
        </form>
        <form class="form-inline edit-form" action="editForm.html" name="edit" method="post">
          <input type="text" class="edit-text form-control" name="password" value="" autocomplete="off" placeholder="修改密码">
          <button class="btn btn-primary" type="button" name="button" onclick="editPassword()">确认修改</button>
          <span class="edit-message"></span>
        </form>
      </div>
      <table class="table table-hover table-bordered">
          <thead>
          <tr>
              <th>密码</th>
              <th width="180">{:lang('ACTIONS')}</th>
          </tr>
          </thead>
          <tbody>
            <foreach name="list" item="item">
              <tr>
                <td>{$item.password}</td>
                <td>
                  <a href="javascript:editBtn('{:url("editForm", ["id"=>$item["id"]])}', '{$item.password}');">修改</a>
                  <a href="{:url('delete',array('id'=>$item['id']))}" class="js-ajax-delete">{:lang('DELETE')}</a>
                </td>
              </tr>
            </foreach>
          </tbody>
      </table>
    </div>
</div>
<script type="text/javascript">
var house_id = '{$house_id}';
var addUrl = '{:url("AdminHousePassword/addForm")}';
var editUrl = '';

function addPassword() {
  var password = add.password.value
  $.post(addUrl, {
      house_id: house_id,
      password: password
    }, function (result) {
      if (result.code === 1) {
        window.location.reload();
      } else {
        $('.add-message').addClass('error');
        $('.add-message').text(result.msg);
        setTimeout(function () {
          $('.add-message').removeClass('error');
          $('.add-message').text('');
        }, 2000);
      }
    })
}

function editBtn(url, value) {
  editUrl = url;
  $('.add-form').css({'display': 'none'});
  $('.edit-form').css({'display': 'block'});
  $('.edit-form .edit-text').val(value);
  $('.edit-form .edit-text').focus();
}

function editPassword() {
  var password = edit.password.value
  editUrl = 'http://localhost:90/user/verification_code/send.html'
  $.post(editUrl, {
      house_id: house_id,
      password: password
    }, function (result) {
      if (result.code === 1) {
        window.location.reload();
      } else {
        $('.edit-message').addClass('error');
        $('.edit-message').text(result.msg);
        setTimeout(function () {
          $('.edit-message').removeClass('error');
          $('.edit-message').text('');
        }, 2000);
      }
    })
}
</script>
<script src="__STATIC__/js/admin.js"></script>
</body>
</html>
